package edu.vt.cbil.output;

import edu.vt.cbil.dataset.*;

import java.io.*;
import java.util.ArrayList;

/**
 * Created by SunMing on 2016/6/29.
 */
public class WriteFile {
    private ArrayList<ResultData> resultDatas=new ArrayList<>();
    private ArrayList<SNPInfo> snpInfos=new ArrayList<>();
    private ConfigObject conf=new ConfigObject();
    private String type;
    private String SAICResultPath;
    private ArrayList<QueryData> queryDatas=new ArrayList<>();
    private ArrayList<SCAGene> SCAGenes=new ArrayList<>();

    public WriteFile(ArrayList<ResultData> resultDatas,ArrayList<SNPInfo> snpInfos,ConfigObject conf,String type){
        this.resultDatas=resultDatas;
        this.snpInfos=snpInfos;
        this.conf=conf;
        this.type=type;
    }

    public void write(){
        SAICResultPath=conf.getSAICResultPath();
        queryGene();
        String line;

        //write the SCAs
        File SCAsFile=new File(SAICResultPath+conf.getChrName()+"_"+type+"_SCAs");
        try {
            BufferedWriter bw = new BufferedWriter(new FileWriter(SCAsFile));

            //write the first line
            line="chr"+"\t"+"start"+"\t"+"end";
            bw.write(line);
            bw.newLine();

            //write the datas
            for (QueryData queryData:queryDatas){
                line=queryData.getChrName()+"\t"+queryData.getStart()+"\t"+queryData.getEnd();
                bw.write(line);
                bw.newLine();
            }
            bw.close();
        }
        catch (Exception e){
            e.printStackTrace();
        }

        //write the SCAGene
        File geneFile=new File(SAICResultPath+conf.getChrName()+"_"+type+"_genes");
        try {
            BufferedWriter bw=new BufferedWriter(new FileWriter(geneFile));

            line="geneId"+"\t"+"geneName";
            bw.write(line);
            bw.newLine();
            for (SCAGene SCAGene:SCAGenes){
                line=SCAGene.getGeneId()+"\t"+SCAGene.getGeneName();
                bw.write(line);
                bw.newLine();
            }
            bw.close();
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }

    public void queryGene(){
        //generate the queryDatas according to the resultDatas and snpInfos
        int startId,endId;
        for (ResultData resultData:resultDatas){
            startId=resultData.getStartId();
            endId=resultData.getEndId();
            QueryData tempQueryData=new QueryData();
            tempQueryData.setChrName(snpInfos.get(startId).getChrName());
            tempQueryData.setStart(snpInfos.get(startId).getPos());
            tempQueryData.setEnd(snpInfos.get(endId).getPos());
            queryDatas.add(tempQueryData);
        }
        //System.out.println("OK");

        //get the geneDatas
        GeneData geneData=new GeneData();
        geneData.readGeneInfos(conf.getQueryFile());

        //query starts
        int startIndex,endIndex,SCAStart,SCAEnd;
        for (QueryData queryData:queryDatas){
            SCAStart=queryData.getStart();
            SCAEnd=queryData.getEnd();

            startIndex=geneData.getGeneChr().indexOf(queryData.getChrName());
            endIndex=geneData.getGeneChr().lastIndexOf(queryData.getChrName());

            for(int i=startIndex;i<endIndex;i++){
                if((geneData.getGeneStart().get(i)<SCAEnd)&&(geneData.getGeneEnd().get(i)>SCAStart)){
                    SCAGene tempSCAGene=new SCAGene();
                    tempSCAGene.setGeneId(geneData.getGeneId().get(i));
                    tempSCAGene.setGeneName(geneData.getGeneName().get(i));
                    SCAGenes.add(tempSCAGene);
                }
            }
        }
    }
}
